package com.demo.word.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.demo.word.DataTable;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * @Author likz
 * @Date 2024/12/18 17:13
 * @Description
 **/
public  class WordContentDemo {



    @Test
    public  void test2(){
        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\likz\\Desktop\\健帆\\CTCAE500.docx"));

            int num = 0;
            // 读取表格
            for (XWPFTable table : document.getTables()) {
                System.out.println("====第"+num+"表格开始====\n");

                Map<String, Object> tabMap=new LinkedHashMap<>();
                Map<String, List<DataTable>> dataMap=new LinkedHashMap<>();
                for (int row = 0; row < table.getNumberOfRows(); row++) {
                    System.out.println("====第"+row+"行开始====\n");
                    XWPFTableRow xwpfTableRow = table.getRow(row);
                    if(row==0){
                        String text = xwpfTableRow.getCell(0).getText();
                        tabMap.put("title",text);
                    }else if(row==1){
                        List<String> headList=new ArrayList();
                        for (int col = 0; col < xwpfTableRow.getTableCells().size(); col++) {
                            XWPFTableCell cell = xwpfTableRow.getCell(col);
                            // 打印单元格信息
                            String cellText = cell.getText();
                            headList.add(cellText);
                        }
                        tabMap.put("head",headList);
                    }else{
                        // 判断文本中是否包含指定的字段
                        String speText = xwpfTableRow.getTableCells().get(0).getText();

                        if(StrUtil.isNotEmpty(speText)&&speText.contains("定义")){
                            List<String> speList=new ArrayList<>();
                            String[] split = StrUtil.split(speText,"引申注释");
                            if(CollUtil.isNotEmpty(speList)){
                                speList.add(split[0]);
                                if (split.length > 1) {
                                    speList.add(split[1]);
                                }
                            }
                           
                            tabMap.put("speList",speList);
                        }else{
                            List<DataTable> dataTables=new ArrayList<>();
                            for (int col = 0; col < xwpfTableRow.getTableCells().size(); col++) {
                                DataTable dataTable = new DataTable();
                                XWPFTableCell cell = xwpfTableRow.getCell(col);
                                // 获取跨列数
                                int colSpan = 1;
                                if (cell.getCTTc().getTcPr().isSetGridSpan()) {
                                    colSpan = cell.getCTTc().getTcPr().getGridSpan().getVal().intValue();
                                }
                                // 获取跨行数
                                int rowSpan = 1;
                                // 打印单元格信息
                                String cellText = cell.getText();

                                System.out.println("单元格(" + row + "," + col + "): 文本=" + cellText + ", 列合并=" + colSpan + ", 行合并=" + rowSpan);
                                dataTable.setCell(String.valueOf(col));
                                dataTable.setCellValue(cellText);
                                dataTables.add(dataTable);
                            }

                                dataMap.put(String.valueOf(row),dataTables);

                        }

                    }
                    System.out.println("dataMap:"+dataMap);
                    System.out.println("\n====第"+row+"行结束====");
                }
                tabMap.put("dataMap",dataMap);
                System.out.println("====第"+num+"表格结束====\n");
                num++;
                break;

            }

            document.close();


        }catch (Exception e){

        }
    }

    @Test
    public void test33(){
        String st="定义：100mL            血液中的血红蛋白总量降低为特征的疾病。贫血的体征和症状包括：皮肤和粘膜苍白，短促呼吸，心悸，柔和的收缩期杂音，倦怠和易疲劳。引申注释：—";
        String[] split = st.split("(引申注释:.*)",2);
        System.out.println("前半部分: " + split[0]);
        if (split.length > 1) {
            System.out.println("后半部分: " + split[1]);
        }
    }


    public static void main(String[] args) {
        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\likz\\Desktop\\健帆\\CTCAE500.docx"));


            int pageNumber = 1;
            boolean newPage = true;
            // 读取分页
//            for (XWPFParagraph paragraph : document.getParagraphs()) {
//                String text = paragraph.getText();
//                if (text != null && text.contains("\u000B")) { // '\u000B' 是Word中分页的特殊字符
//                    newPage = true;
//                    continue;
//                }
//                XWPFDocument document1 = paragraph.getDocument();
//            }
            int num = 0;
            // 读取表格
            for (XWPFTable table : document.getTables()) {
                System.out.println("====第"+num+"表格开始====\n");
                List<List> rowList=new ArrayList();
                for (int row = 0; row < table.getNumberOfRows(); row++) {
                    List<Map> cellList=new ArrayList();
                    System.out.println("====第"+row+"行开始====\n");
                    XWPFTableRow xwpfTableRow = table.getRow(row);

                    for (int col = 0; col < xwpfTableRow.getTableCells().size(); col++) {
                        Map<String, Object> map=new LinkedHashMap<>();
                        XWPFTableCell cell = xwpfTableRow.getCell(col);
                        // 获取跨列数
                        int colSpan = 1;
                        if (cell.getCTTc().getTcPr().isSetGridSpan()) {
                            colSpan = cell.getCTTc().getTcPr().getGridSpan().getVal().intValue();
                        }
                        // 获取跨行数
                        int rowSpan = 1;
                        // 打印单元格信息
                        String cellText = cell.getText();
                        System.out.println("单元格(" + row + "," + col + "): 文本=" + cellText + ", 列合并=" + colSpan + ", 行合并=" + rowSpan);
                        map.put("row",row);
                        map.put("col",col);
                        map.put("text",cellText);
                        cellList.add(map);
                    }
                    rowList.add(cellList);
                    System.out.println("\n====第"+row+"行结束====");
                }
                //System.out.println(rowList);
                System.out.println("====第"+num+"表格结束====\n");
                num++;
                break;

            }

            document.close();


        }catch (Exception e){

        }

    }
    //@Test
    public  void testPage() {
        String filePath = "C:\\Users\\likz\\Desktop\\健帆\\CTCAE51.docx"; // 建议使用配置文件管理此路径

        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis)) {

            int pageNumber = 1;
            boolean newPage = true;

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String text = paragraph.getText();
                if (text != null && text.contains("\u000B")) { // '\u000B' 是Word中分页的特殊字符
                    System.out.println("分页符位于段落: " + text);
                    newPage = true;
                    continue;
                }

                if (newPage) {
                    System.out.println("第 " + pageNumber + " 页的表格数据:");
                    newPage = false;
                    pageNumber++;
                }

                for (XWPFTable table : document.getTables()) {
                    for (int row = 0; row < table.getNumberOfRows(); row++) {
                        System.out.println("====第"+row+"行开始====\n");
                        for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) {
                            XWPFTableCell cell = table.getRow(row).getCell(col);
                            String cellText = cell.getText();
                            System.out.print(cellText + "\t");
                        }
                        System.out.println("\n====第"+row+"行结束====");
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //@Test
    public void test1(){

        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\likz\\Desktop\\健帆\\CTCAE51.docx"));

            // 读取表格
            for (XWPFTable table : document.getTables()) {

                for (int row = 0; row < table.getNumberOfRows(); row++) {
                    XWPFTableRow tableRow = table.getRow(row);
                    CTRow ctRow = tableRow.getCtRow();
                    List<XWPFTableCell> tableCells = tableRow.getTableCells();

                    System.out.println("====第"+row+"行开始====\n");
                    for (int col = 0; col < tableCells.size(); col++) {
                        XWPFTableCell cell = table.getRow(row).getCell(col);
                         // 列索引从0开始
                            String cellText = cell.getText();
                            System.out.print(cellText + "");

                    }
                    System.out.println("\n====第"+row+"行结束====");
                }

            }

            document.close();


        }catch (Exception e){

        }





    }







}
